package com.pyg.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pyg.manager.service.BrandService;
import com.pyg.pojo.TbBrand;
import com.pyg.utils.PageResult;
import com.pyg.utils.PygResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Reference(timeout = 1000000)
    private BrandService brandService;

    /**
     * 查询所有品牌
     */
    @RequestMapping("/findAll")
    public List<TbBrand> findAll() {
        return brandService.findAll();
    }

    /**
     * 品牌分页查询展示
     */
    @RequestMapping("/findByPage/{page}/{rows}")
    public PageResult findByPage(@PathVariable Integer page,
                                 @PathVariable Integer rows) {
        PageResult result = brandService.findByPage(page, rows);
        return result;
    }

    /**
     * 新增品牌数据
     */
    @RequestMapping("/add")
    public PygResult add(@RequestBody TbBrand brand) {
        try {
            brandService.add(brand);
            return new PygResult(true, "保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false, "保存失败!");
        }
    }

    /**
     * 通过id查询品牌数据
     */
    @RequestMapping("/findById")
    public TbBrand findById(long id) {
        return brandService.findById(id);
    }

    /**
     * 修改品牌数据
     */
    @RequestMapping("/update")
    public PygResult update(@RequestBody TbBrand brand) {
        try {
            brandService.updateByPrimaryKey(brand);
            return new PygResult(true, "保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false, "保存失败!");
        }
    }

    /**
     * 批量删除品牌数据
     */
    @RequestMapping("/delete/{ids}")
    public PygResult delete(@PathVariable long[] ids) {
        try {
            brandService.delete(ids);
            return new PygResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(false, "删除失败");
        }
    }
}
